package com.chen.bpsptsystem.service;

import com.chen.bpsptsystem.common.ServerResponse;
import com.chen.bpsptsystem.dao.MyMapper;
import com.chen.bpsptsystem.dao.UserClazzMapper;
import com.chen.bpsptsystem.dao.UserInteMapper;
import com.chen.bpsptsystem.dao.UserMapper;
import com.chen.bpsptsystem.po.*;
import com.chen.bpsptsystem.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 */
@Service
public class IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserInteMapper userInteMapper;

    @Resource
    private UserClazzMapper userClazzMapper;

    @Resource
    private MyMapper myMapper;

    public ServerResponse register(User user, String inteId) {
        if (!checkAmount(user.getAmount())) {
            return ServerResponse.createByErrorMsg("账号已存在");
        }
        User newUser = new User();
        BeanUtils.copyProperties(user, newUser);
        newUser.setType(0);
        newUser.setPassword(MD5Util.encode(user.getPassword()));
        newUser.setStatus((byte) 0);
        int row = userMapper.insertSelective(newUser);
        if (row > 0) {
            if (StringUtils.isNotEmpty(inteId)) {
                UserInte userInte = new UserInte();
                userInte.setInteId(inteId);
                userInte.setUserId(newUser.getId());
                userInteMapper.insertSelective(userInte);
            }
            return ServerResponse.createBySuccess(newUser);
        }
        return ServerResponse.createByErrorMsg("注册失败");
    }

    private boolean checkAmount(String amount) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAmountEqualTo(amount);
        List<User> users = userMapper.selectByExample(example);
        return !CollectionUtils.isNotEmpty(users);
    }

    public ServerResponse login(String amount, String password) {
        if (checkAmount(amount)) {
            return ServerResponse.createByErrorMsg("用户名不存在");
        }
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAmountEqualTo(amount);
        criteria.andPasswordEqualTo(MD5Util.encode(password));
        List<User> user = userMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(user)) {
            return ServerResponse.createByErrorMsg("用户名或者密码错误");
        }
        return ServerResponse.createBySuccess(user.get(0));
    }

    public ServerResponse add(User user, String clazzId) {
        if (!checkAmount(user.getAmount())) {
            return ServerResponse.createByErrorMsg("账号已存在");
        }
        User newUser = new User();
        BeanUtils.copyProperties(user, newUser);
        newUser.setType(0);
        newUser.setPassword(MD5Util.encode(user.getPassword()));
        newUser.setStatus((byte) 0);
        int row = userMapper.insertSelective(newUser);
        if (row > 0) {
            if (StringUtils.isNotEmpty(clazzId)) {
                UserClazz userClazz = new UserClazz();
                userClazz.setClazzId(clazzId);
                userClazz.setUserId(newUser.getId());
                userClazzMapper.insertSelective(userClazz);
            }
            return ServerResponse.createBySuccess(newUser);
        }
        return ServerResponse.createByErrorMsg("添加失败");
    }

    /**
     * 用户list
     *
     * @param pn
     * @param pageSize
     * @return
     */
    public ServerResponse list(Integer pn, Integer pageSize) {
        PageHelper.startPage(pn, pageSize);
        List<User> users = userMapper.selectByExample(null);
        PageInfo<User> info = new PageInfo<>(users);
        return ServerResponse.createBySuccess(info);
    }

    public ServerResponse delete(Integer userId) {
        int row = userMapper.deleteByPrimaryKey(userId);
        if (row > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMsg("删除失败");
    }

    public ServerResponse editUserClazz(Integer userId, String clazzId) {
        User user = userMapper.selectByPrimaryKey(userId);
        if (null == user) {
            return ServerResponse.createByErrorMsg("用户不存在");
        }
        UserClazz userClazz = new UserClazz();
        userClazz.setClazzId(clazzId);
        userClazz.setUserId(userId);
        UserClazzExample example = new UserClazzExample();
        UserClazzExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        int row = userClazzMapper.updateByExampleSelective(userClazz, example);
        if (row > 0) {
            return ServerResponse.createBySuccessMsg("修改成功");
        }
        return ServerResponse.createByErrorMsg("修改失败");
    }

    public ServerResponse myMaterialList(Integer pn, Integer pageSize, Integer userId) {
        PageHelper.startPage(pn, pageSize);
        List<Map<String, Object>> mapList = myMapper.myMaterialList(userId);
        PageInfo info = new PageInfo(mapList);
        return ServerResponse.createBySuccess(info);
    }

    public ServerResponse userInfo(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return ServerResponse.createBySuccess(user);
    }
}
